ЛАБОРАТОРНАЯ РАБОТА № 2
«Создание простейшей визуальной программы на 
JAVA»

 

Исходный код примера: 

package tsn.javase.lab01;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
public class Form1 extends javax.swing.JFrame {
    public Form1() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    //                          
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Создание простейшей визуальной программы на  JAVA ");
        setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getResource("icon.png")));
        setResizable(false);
        getContentPane().setLayout(null);
        jLabel1.setText("B");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 50, 6, 14);
        jLabel2.setText("D");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 80, 7, 14);
        jLabel3.setText("X");
        jLabel3.setToolTipText("");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 20, 6, 14);
        getContentPane().add(jTextField1);
        jTextField1.setBounds(20, 20, 59, 20);
        getContentPane().add(jTextField2);
        jTextField2.setBounds(20, 50, 59, 20);
        getContentPane().add(jTextField3);
        jTextField3.setBounds(20, 80, 59, 20);
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tsn/javase/lab01/variant.jpg"))); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(90, 20, 380, 90);
        jButton1.setText("Выход");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(320, 120, 140, 30);
        jButton2.setText("Очистить");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(170, 120, 140, 30);
        jButton3.setText("Решить");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(10, 120, 150, 30);
        jLabel5.setText("ОТВЕТ:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(10, 160, 450, 14);
        jMenu1.setText("Решить");
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu1MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu1);
        jMenu2.setText("Очистить");
        jMenu2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu2MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu2);
        jMenu3.setText("ВЫХОД");
        jMenu3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu3MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu3);
        setJMenuBar(jMenuBar1);
        setSize(new java.awt.Dimension(487, 234));
        setLocationRelativeTo(null);
    }//                        
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Решение примера
        float x, b, d, y; // Вещественные переменные
        try {
            x = Float.parseFloat(jTextField1.getText()); // Получение данных
            b = Float.parseFloat(jTextField2.getText()); // Получение данных
            d = Float.parseFloat(jTextField3.getText()); // Получение данных
        } catch (Exception ex) {
            Toolkit.getDefaultToolkit().beep(); // Издаем звук
            // Выводим окошко с сообщением об ошибке
            JOptionPane.showMessageDialog(rootPane, "Ошибка введенных данных!", "Ошибка ввода",
                    JOptionPane.ERROR_MESSAGE);
            jTextField1.requestFocus(); // Устанавливаем фокус на компонент
            jLabel5.setText("В введенных значениях допущены ошибки");
            jTextField1.setText(""); // Очистка данных
            jTextField2.setText(""); // Очистка данных
            jTextField3.setText(""); // Очистка данных
            return; // Выход из метода (процедуры)
        }
        if (x >= 8) { // Вычисление выражения
            y = (x - 2) / (x * x);
        } else {
            y = b * b * d + 4 * x * x * x;
        }
        jLabel5.setText("ОТВЕТ: " + String.format("%.2f", y)); // Выдача ответа с двумя знаками после запятой
    }                                       
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Выход из программы
        System.exit(0);
    }                                        
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Очистка полей ввода
        jLabel5.setText("ОТВЕТ:");
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
    }                                       
    private void jMenu3MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        jButton1ActionPerformed(null);
    }                                  
    private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // Меню вызова очистки полей ввода
        jButton2ActionPerformed(null);
    }                                   
    private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {                                   
        // Меню вызова решения примера
        jButton3ActionPerformed(null);
    }                                  
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration                  
}